package com.LearnInWorld.handler;

import com.LearnInWorld.entity.Result;
import com.LearnInWorld.entity.StatusCode;
import com.LearnInWorld.exception.CustomException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
 */
@ControllerAdvice
public class BaseExceptionHandler {


    /**
     * 自定义异常
     * @param ce
     * @return
     */
    @ExceptionHandler(value = CustomException.class)
    @ResponseBody
    public Result error(CustomException ce){
        return new Result(ce.isFlag(),ce.getCode(),ce.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result error(Exception e) {
        e.printStackTrace();
        return new Result(false, StatusCode.ERROR, "执行错误");
    }
}
